package com.testapp.bluetoothtest08.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Looper;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSONObject;
import com.testapp.bluetoothtest08.Constant.Constants;
import com.testapp.bluetoothtest08.HomeActivity;
import com.testapp.bluetoothtest08.Http.OkHttp;
import com.testapp.bluetoothtest08.R;
import com.testapp.bluetoothtest08.Util.AppUtil;

public class ModifyPasswordDialog extends Dialog {
    private EditText old_pwd, new_pwd;
    private Button modify_confirm ,modify_cancel;
    private String oldPwd,newPwd;
    private AppUtil appUtil=new AppUtil();

    public ModifyPasswordDialog(@NonNull Context context) {
        super(context);
    }

    public ModifyPasswordDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_password);
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        Point size = new Point();
        d.getSize(size);
        p.width = (int) (size.x * 0.8);//设置宽度为当前宽度的百分之80 getWindow().setAttributes(p);
        init();
    }

    private void init() {
        old_pwd = findViewById(R.id.old_pwd);
        new_pwd = findViewById(R.id.new_pwd);
        modify_confirm = findViewById(R.id.modify_confirm);
        modify_cancel =findViewById(R.id.modify_cancel);

        modify_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                oldPwd = old_pwd.getText().toString();
                newPwd = new_pwd.getText().toString();

                JSONObject j = new JSONObject();
                j.put("oldPwd",oldPwd);
                j.put("newPwd",newPwd);
                System.out.println(j.toJSONString());
                OkHttp http = new OkHttp();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String re = http.httpPostWithToken(Constants.MODIFY_PASSWORD_URL, j.toJSONString());
                        System.out.println("修改密码 ---- 服务器返回 -----> "+re);
                        if(re!=null){
                            if(re.contains("成功")){

                                Looper.prepare();
                                appUtil.logout(getContext());
                                Toast.makeText(getContext(), "success!!", Toast.LENGTH_SHORT).show();
                                Looper.loop();

                            }else{
                                Looper.prepare();
                                Toast.makeText(getContext(),"error old password",Toast.LENGTH_SHORT).show();
                                Looper.loop();
                            }
                        }else{
                            Looper.prepare();
                            Toast.makeText(getContext(),"error",Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }
                    }
                }).start();
                dismiss();
            }
        });

        modify_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }
}
